package servlet;

import com.ncist.dao.MessagesDao;
import com.ncist.dao.yaoguaiDao;
import com.ncist.pojo.Messages;
import com.ncist.pojo.yaoguai;
import com.ncist.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/Tougao")
public class TougaoServlet extends HttpServlet {
    SqlSession sqlSession;
@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        sqlSession = MybatisUtils.getSqlSession();
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String wechat = request.getParameter("wechat");
        String leixi = request.getParameter("grade");
        String content = request.getParameter("con");
        String img = request.getParameter("uploadFile");
        System.out.println(sex);
        String a="image/";
        img=a+img;

        yaoguaiDao mapper = sqlSession.getMapper(yaoguaiDao.class);
        if(sex == null){
            sex = "";
        }

        if (sex.equals("女")) {
            List<yaoguai> x=mapper.insertmm(name,wechat);
            List<yaoguai> y = mapper.insert(name,leixi,content,img);
            response.sendRedirect("join.jsp");
        }
        else {
            List<yaoguai> y = mapper.insert(name,leixi,content,img);
            response.sendRedirect("join.jsp");
        }

        //第一步：获得sqlSession对象
        //第二步:获得Mapper接口的代理对象
        //第三步:执行方法
        //关闭sqlSession
        sqlSession.close();
    }

@Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
